package com.eims.sls_app.ui;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.GamecenterActivity.MyAsynTask;
import com.eims.sls_app.ui.GamecenterActivity.MyHandler;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.App;

import bls.juejin.cn.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RedPacketActivity extends MyBaseActivity implements OnClickListener {
	private static final String TAG = "RedPacketActivity";
	private String title;
	private String SchemeNumber;
	private ImageView iv_close,iv_pic;
	private TextView tv_zhongjiang,tv_qian,tv_hongbao,tv_yuan,tv_look;
	private LinearLayout ll_look;
	private int pic;
	private MyHandler myHandler;
	private MyAsynTask myAsynTask;
	private String opt, auth, info, time, imei, crc, key; // 格式化后的参数
	//private static int ERROR = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_red_packet);
		App.activityS.add(this);
		//App.activityS1.add(this);
		//RedPacketActivity.this.setFinishOnTouchOutside(false);  
		initView();
		initData();
		setListener();
	}

	private void setListener() {
		iv_close.setOnClickListener(this);
		/*ll_look.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RedPacketActivity.this.finish();
				Intent intent = new Intent(getApplicationContext(),
						MainActivity.class);
				RedPacketActivity.this.startActivity(intent);
				MainActivity.toCenter();
			}
		});*/
		
		tv_look.setOnClickListener(this);
		
	}

	private void initData() {
		Intent intent = getIntent();
		title = intent.getStringExtra("money");
		SchemeNumber = intent.getStringExtra("SchemeNumber");
		pic = intent.getIntExtra("pic", 0);
		myAsynTask = new MyAsynTask();
		myAsynTask.execute();
		myHandler = new MyHandler();
		
		iv_pic.setImageResource(pic);
		if (title.equals("0")) {
			tv_qian.setVisibility(View.GONE);
			tv_zhongjiang.setText("很遗憾未中奖!");
			tv_hongbao.setText("下次再接再厉");
			tv_yuan.setVisibility(View.GONE);
		}else {
			tv_qian.setText(title);
		}
		//tv_zhongjiang.setText(SchemeNumber);
		
	}
	
	@Override  
    public boolean onTouchEvent(MotionEvent event) {  
        if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {  
            return true;  
        }  
        return super.onTouchEvent(event);  
    }  
  
    private boolean isOutOfBounds(Activity context, MotionEvent event) {  
        final int x = (int) event.getX();  
        final int y = (int) event.getY();  
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();  
        final View decorView = context.getWindow().getDecorView();  
        return (x < -slop) || (y < -slop)|| (x > (decorView.getWidth() + slop))|| (y > (decorView.getHeight() + slop));  
    }  

	private void initView() {
		iv_close = (ImageView) findViewById(R.id.iv_close);
		tv_zhongjiang = (TextView) findViewById(R.id.tv_zhongjiang);
		ll_look = (LinearLayout) findViewById(R.id.ll_look_over);
		tv_qian = (TextView) findViewById(R.id.tv_qian);
		tv_hongbao = (TextView) findViewById(R.id.tv_hongbao);
		iv_pic = (ImageView) findViewById(R.id.iv_pic);
		tv_yuan = (TextView) findViewById(R.id.tv_yuan);
		tv_look = (TextView)findViewById(R.id.tv_look);
		
	}
	
	/** 异步任务 */
	class MyAsynTask extends AsyncTask<Void, Integer, String> {

		

		

		/** 在后台执行的程序 */
		@Override
		protected String doInBackground(Void... params) {
			int p;
			
			info = RspBodyBaseBean.changeCJ_info(SchemeNumber, title);

			//Log.i("x", "获得我全部投注记录    info  " + info);

			opt = "84";
			crc = RspBodyBaseBean.getCrc(time, imei, key, info,
						App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei,
						App.user.getUid());
			Log.i(TAG,"抽奖>"+ RequestParams.convertParams(RedPacketActivity.this, opt, info));
			String[] values = { opt, auth, info };
			String result = HttpUtils.doPost(App.names, values,
					App.path);

			Log.e("x", "得到所有抽奖内容   " + result);
			// MyToast.getToast(context, result);

			if ("-500".equals(result))
				return result;

			if (null == result)
				return "1";
			
			if (result.equals("success")) {
				return "0";
			}else {
				try {
					JSONObject item = new JSONObject(result);
					String ERROR = item.optString("error");
					return ERROR;
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

			return "0";
		}

		@Override
		protected void onPostExecute(String result) {
			if (null == result)
				return;
			if ("1".equals(result)) {
				myHandler.sendEmptyMessage(-1);
			}

			myHandler.sendEmptyMessage(Integer.parseInt(result));
			super.onPostExecute(result);
		}
	}
	
	/** 进行页面刷新 */
	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				//getText2(count);
				break;
			case -1:
				
				break;
			
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_close:
			this.setResult(RESULT_OK);
			finish();
			break;
		case R.id.tv_look:
			RedPacketActivity.this.finish();
			for (int i = 0; i < App.activityS1.size(); i++) {
				App.activityS1.get(i).finish();
			}
			MainActivity.toCenter();
			break;

		default:
			break;
		}
		
	}

}
